package org.robot.scm.platform.server.handler;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.CompletableFuture;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.robot.scm.infra.LocalCacheHelper;
import org.robot.scm.pojos.config.BaseConfig;
import org.robot.scm.pojos.enums.PlatformEnum;
import org.robot.scm.utils.RequestHelper;

/**
 * AWS服务重发
 */
@Slf4j
@NoArgsConstructor
public class DeployAwsHandler extends BaseHandler implements HttpHandler {

    @Override
    public String getUri() {
        return "/deployAws";
    }

    @Override
    public void handle(HttpExchange exchange) throws IOException {
        // 先发布机器人服务端, 并同步等待返回结果
        String robotResult = RequestHelper.get("http://47.99.155.172:8205/deployRobot?auth=d249159b3fa944e0b2457a6694930f47");
        log.info("end robot, result : {}", robotResult);

        // 再发布机器人客户端， 这里有个frps尚未启动，等待N秒再执行
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                // 当启动一个新的进程的时候当前系统直接退出
                ProcessBuilder processBuilder = new ProcessBuilder("cmd", "/c", "start", "/min", "start.bat");
                try {
                    processBuilder.start();
                    System.exit(0);
                } catch (IOException e) {
                    log.error("error", e);
                }
            }
        }, 30 * 1000L);

        sendSuccessResponse(exchange, "服务发布完成");
    }

}
